clear
rep=1;
%% Problem
d=2;
x_bound=repmat([-6,6],[d,1]);
precision = 0.024;
NewRegionNum = 2;
TotalBudget = 3000;
MaxPartitionDepth = sum(ceil(log((x_bound(:,2)-x_bound(:,1))./precision)...
    ./log(NewRegionNum)))

Problem.Dimension = d;
Problem.Domain = reshape(x_bound',[1,d*2]);
Problem.Sampling = @Sampling_BoxConstraint;

%% Algorithm
AlgorithmM.n0 = 4;
AlgorithmM.QuantileLevel = 0.3;
AlgorithmM.NewBudget = 3;
AlgorithmM.MaxSampleSize = 10;
AlgorithmM.StopCriteria = [1,TotalBudget];
ClearRadial = precision*2;
%%
Problem.Partition = @(Region,SampleSet)Partition_NP_BoxConstraint(Region, NewRegionNum, MaxPartitionDepth, SampleSet);
time1=tic;
for i=1:rep
[RegionSet, SampleSet, RegionSampleId] = PRS_MMO( Problem, AlgorithmM );
[Optima] = Niching(RegionSet,SampleSet,RegionSampleId,ClearRadial);
end
toc(time1)

%% FIGURES
if rep == 1
    %% FIGURE 1
    if d == 2
        figure()
        hold on
        rectangle('Position',[Problem.Domain([1,3]),Problem.Domain([2,4])-Problem.Domain([1,3])])
        scatter(SampleSet(:,2),SampleSet(:,3),2,SampleSet(:,1),'filled');
        for i=1:length(RegionSet)
            rectangle('Position',[RegionSet(i,[3,5]),RegionSet(i,[4,6])-RegionSet(i,[3,5])])
        end
        title('','Interpreter','latex','String',['$N=',num2str(TotalBudget),'$'])
        xlabel('','Interpreter','latex','String','$x_1$')
        ylabel('','Interpreter','latex','String','$x_2$')
        set(gca,'Fontname','Times New Roman','FontSize',16);
        hold off
        figure()
        hold on
        rectangle('Position',[Problem.Domain([1,3]),Problem.Domain([2,4])-Problem.Domain([1,3])])
        scatter(Optima(:,2),Optima(:,3),5,'filled');
        hold off
    end
    %% FIGURE 2 WE NEED RESAMPLING HERE
%     samplevalues = cell(MaxPartitionDepth,1);
%     figure()
%     samplevalue = [SampleSet(:,2),zeros(size(SampleSet,1),1)];
%     for i=1:length(RegionSet)
%         samplevalue(RegionSampleId{i},2) = RegionSet(i,1);
%     end
%     boxplot(samplevalue(:,1),samplevalue(:,2))
end
save('data.mat')
